package org.fullhappy.rest.entities;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.fullhappy.dao.CategoryDAO;
import org.fullhappy.dao.ContactInfoDAO;
import org.fullhappy.dao.GalleryDAO;
import org.fullhappy.dao.ProductDAO;
import org.fullhappy.entity.Category;
import org.fullhappy.entity.ContactInfo;
import org.fullhappy.entity.Gallery;
import org.fullhappy.entity.Product;
import org.fullhappy.rest.MediaType;
import org.fullhappy.rest.annotation.Consumes;
import org.fullhappy.rest.annotation.FormParam;
import org.fullhappy.rest.annotation.POST;
import org.fullhappy.rest.annotation.Path;
import org.fullhappy.rest.annotation.Produces;

import com.google.appengine.api.datastore.Text;
import com.google.gson.Gson;

@Path("/contact")
public class ContactREST {

	@POST
	@Path("/update")
	// @Consumes(MediaType.MULTIPART_FORM_DATA)
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Produces("application/json")
	public String updateContact(@FormParam("name") String name,
			@FormParam("phone") String phone, @FormParam("email") String email,
			@FormParam("web") String web, @FormParam("yahoo") String yahoo,
			@FormParam("skype") String skype,
			@FormParam("facebook") String facebook,
			@FormParam("address") String address,
			@FormParam("latitude") Double latitude,
			@FormParam("longitude") Double longitude) {

		ContactInfoDAO contactInfoDAO = new ContactInfoDAO();
		List<ContactInfo> contactInfos = contactInfoDAO.findAll();
		ContactInfo contactInfo = null;
		if (contactInfos != null && !contactInfos.isEmpty()) {
			contactInfo = contactInfos.get(0);
		} else {
			contactInfo = new ContactInfo();
		}

		if (name != null) {
			contactInfo.setName(name);
		}
		
		if (phone != null) {
			contactInfo.setPhone(phone);
		}
		if (email != null) {
			contactInfo.setEmail(email);
		}
		if (web != null) {
			contactInfo.setWeb(web);
		}
		if (yahoo != null) {
			contactInfo.setYahoo(yahoo);
		}
		if (skype != null) {
			contactInfo.setSkype(skype);
		}
		if (facebook != null) {
			contactInfo.setFacebook(facebook);
		}
		if (address != null) {
			contactInfo.setAddress(address);
		}

		if (latitude != null) {
			contactInfo.setLatitude(latitude);
		}
		
		if (longitude != null) {
			contactInfo.setLongitude(longitude);
		}

		try {
			if (contactInfos != null && !contactInfos.isEmpty()) {
				contactInfoDAO.update(contactInfo);
			} else {
				contactInfoDAO.insert(contactInfo);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "{'result':'" + false + "'}";
		}

		return "{'result':'" + (contactInfo != null) + "'}";
	}

	@POST
	@Path("/get_contact")
	// @Consumes(MediaType.MULTIPART_FORM_DATA)
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Produces("application/json")
	public String getContact() {

		ContactInfoDAO contactInfoDAO = new ContactInfoDAO();
		List<ContactInfo> contactInfos = contactInfoDAO.findAll();
		ContactInfo contactInfo = null;
		String result="";
		if (contactInfos != null && !contactInfos.isEmpty()) {
			contactInfo = contactInfos.get(0);
			Gson gson = new Gson();
			result = gson.toJson(contactInfo);
		}
		
		return result;

	}

}
